import 'package:flutter_hsh/fluttergromore/utils/json_util.dart';

import '../../../utils/k_log.dart';

/// 菜谱搜索结果数据模型
/// 用于处理菜谱搜索接口返回的数据

List<RecipeSearchBean> getRecipeSearchBean(dynamic json) {
  List<RecipeSearchBean> recipeList = [];
  if (json != null) {
    json.forEach((v) {
      recipeList.add(RecipeSearchBean.fromJson(v));
    });
  }
  return recipeList;
}

class RecipeSearchBean {
  RecipeSearchBean({
    this.id,
    this.title,
    this.description,
    this.imageUrl,
    this.cookTime,
    this.difficulty,
    this.servings,
    this.category,
    this.ingredients,
    this.steps,
    this.nutrition,
    this.tags,
    this.author,
    this.rating,
    this.createTime,
    this.updateTime,
  });

  RecipeSearchBean.fromJson(dynamic json) {
    // 基础字段映射
    id = json['id'];
    title = json['title'] ?? json['name'] ?? json['category']; // 兼容不同字段名
    description = json['description'] ?? json['desc'];
    imageUrl = json['banner'];
    cookTime = json['cookTime'] ?? json['cook_time'];
    difficulty = json['difficulty'];
    servings = json['servings'];
    category = json['category'] ?? json['type']; // 兼容type字段

    // 处理 ingredients 字段 - 支持字符串和数组格式
    // if (json['ingredients'] != null) {
    //   if (json['ingredients'] is List) {
    //     ingredients = List<String>.from(json['ingredients']);
    //   } else if (json['ingredients'] is String) {
    //     // 如果是字符串，尝试按逗号分割
    //     ingredients = (json['ingredients'] as String).split(',').map((e) => e.trim()).where((e) => e.isNotEmpty).toList();
    //   }
    // } else {
    //   ingredients = null;
    // }


      if (json["ingredients"] != null) {
        ingredients= JsonUtil.getObjList(json["ingredients"], (v)=>Ingredients.fromJson(v));
      }


    // // 处理 steps 字段 - 支持字符串和数组格式
    // if (json['steps'] != null) {
    //   if (json['steps'] is List) {
    //     steps = List<String>.from(json['steps']);
    //   } else if (json['steps'] is String) {
    //     // 如果是字符串，尝试按换行符或分号分割
    //     steps = (json['steps'] as String).split(RegExp(r'[;\n]')).map((e) => e.trim()).where((e) => e.isNotEmpty).toList();
    //   }
    // } else {
    //   steps = null;
    // }

    if (json["steps"] != null) {
      steps= JsonUtil.getObjList(json["steps"], (v)=>Steps.fromJson(v));
    }

    nutrition = json['nutrition'];

    // 处理 tags 字段 - 支持字符串和数组格式
    if (json['tags'] != null) {
      if (json['tags'] is List) {
        tags = List<String>.from(json['tags']);
      } else if (json['tags'] is String) {
        // 如果是字符串，尝试按逗号分割
        tags = (json['tags'] as String).split(',').map((e) => e.trim()).where((e) => e.isNotEmpty).toList();
      }
    } else {
      tags = null;
    }

    author = json['author'];
    rating = json['rating'];
    createTime = json['createTime'] ?? json['create_time'];
    updateTime = json['updateTime'] ?? json['update_time'];

    // 如果某些关键字段为空，设置默认值
    if (title == null || title!.isEmpty) {
      title = category ?? '美味菜谱';
    }

    // 设置默认描述
    if (description == null || description!.isEmpty) {
      description = '精选${category ?? '美食'}，营养丰富，制作简单';
    }

    // 设置默认烹饪时间
    if (cookTime == null || cookTime!.isEmpty) {
      cookTime = '30分钟';
    }

    // 设置默认难度
    if (difficulty == null || difficulty!.isEmpty) {
      difficulty = '简单';
    }

    // 设置默认评分
    if (rating == null) {
      rating = 4.5;
    }

    // 设置默认作者
    if (author == null || author!.isEmpty) {
      author = '美食达人';
    }
  }

  num? id;
  String? title;
  String? description;
  String? imageUrl;
  String? cookTime;
  String? difficulty;
  num? servings;
  String? category;
  List<Ingredients>? ingredients;
  List<Steps>? steps;
  String? nutrition;
  List<String>? tags;
  String? author;
  num? rating;
  String? createTime;
  String? updateTime;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['title'] = title;
    map['description'] = description;
    map['imageUrl'] = imageUrl;
    map['cookTime'] = cookTime;
    map['difficulty'] = difficulty;
    map['servings'] = servings;
    map['category'] = category;
    map['ingredients'] = ingredients;
    map['steps'] = steps;
    map['nutrition'] = nutrition;
    map['tags'] = tags;
    map['author'] = author;
    map['rating'] = rating;
    map['createTime'] = createTime;
    map['updateTime'] = updateTime;
    return map;
  }
}

class Ingredients {
  Ingredients({
    this.name,
    this.amount,});

  Ingredients.fromJson(dynamic json) {
    name = json['name'];
    amount = json['amount'];
  }
  String? name;
  String? amount;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['name'] = name;
    map['amount'] = amount;
    return map;
  }
}

class Steps {
  Steps({
    this.imageUrl,
    this.description,
    this.step,});

  Steps.fromJson(dynamic json) {
    imageUrl = json['image_url'];
    description = json['description'];
    step = json['step'];
  }
  String? imageUrl;
  String? description;
  num? step;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['image_url'] = imageUrl;
    map['description'] = description;
    map['step'] = step;
    return map;
  }

}




